package top.lingkang.fas.admin.web;

import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import top.lingkang.fas.admin.constant.CacheConstants;
import top.lingkang.fas.admin.utils.RedissonUtil;
import top.lingkang.fas.admin.web.impl.CustomLineCaptcha;
import top.lingkang.fas.common.dto.R;

import java.util.concurrent.TimeUnit;

/**
 * @author lingkang
 * created by 2025/7/20 2:03
 */
@Controller
public class CaptchaController {
    private CustomLineCaptcha lineCaptcha;

    @Init
    public void init() {
        lineCaptcha = new CustomLineCaptcha(100, 60);
    }

    @Mapping("/captchaImage")
    public synchronized R captchaImage(Context ctx) throws Exception {
        lineCaptcha.createCode();
        RedissonUtil.set(CacheConstants.CAPTCHA_CODE_KEY + lineCaptcha.getId(), lineCaptcha.getAnswer(), 300, TimeUnit.SECONDS);
        String base64 = lineCaptcha.getImageBase64();
        return R.ok(lineCaptcha.getId(), base64);
    }
}
